<?php
class User extends CI_Model 
{
	function __construct()
    {
        parent::__construct();
    }
    /********************************************************************
    *  Creates a new user in the database with given parameters. 
    *  Logs the user into the system.   
    *
    *********************************************************************/
    // Information should be validated by this point
    public function create($email, $name, $password)
    {
        $salt = substr(md5(microtime()),rand(0,26),8);
      
        $password = $this->hash_pass($password, $salt);
       
    	$data = array(
    		'name' => $name,
            'email' => $email,
            'password' => $password,
            'salt' => $salt);
    	$this->db->insert('users', $data);
        $this->login($email, $password);
    }
    private function hash_pass($password, $salt)
    {
        return $this->encrypt->sha1($password.$salt);

    }
    /**************************************************************
    *
    *   Logs user into system.. 
    *   Returns true if successful
    *
    ***************************************************************/
    public function login($email, $password)
    {
        $vars = array($email);
        $query = $this->db->query('SELECT name, id, password, salt, email FROM users WHERE email = ?', $vars);
        if($query->num_rows() == 0)
        {
            return false;
        }
        else
        {
            $user = $query->row();
            if($user->password == $this->hash_pass($password, $user->salt))
            {   

                $this->set_session($user->email);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    /*****************************************************************
    *
    *   logs user out of system. 
    *
    *******************************************************************/
    public function logout()
    {
        $this->session->unset_userdata();
        $this->session->sess_destroy();
     
    }

    public function set_session($email)
    {
        $vars = array($email);
        $query = $this->db->query("SELECT * FROM users WHERE email = ?", $vars);
        $num_results = $query->num_rows();
        if($num_results > 0)
        {
            $user = $query->row();
            $session_data = array('user_id' => $user->id,
                              'user_name' => $user->name,
                              'user_email' => $user->email,
                              'user_level' => $user->user_level);
             $this->session->set_userdata($session_data);
         

        }
    }
    
    /**********************************************************************
    *	Deletes user with specified $id
    *
    *
    ***********************************************************************/
    public function delete($id)
    {
    	
    }
    public function id_from_email($id)
    {
        $vars = array($id);
        $query = $this->db->query('SELECT id from users where email = ? LIMIT 1', $vars);
        return $query->row()->id;
    }
    public function update()
    {

    }
    /************************************************************************
    *   Takes email and determines if user exists in database
    *   Returns true if so. 
    *
    ***************************************************************************/
    public function is_user($email)
    {
        $vars =  array($email);
        $query = $this->db->query("SELECT * FROM users WHERE email = ?", $vars);
        $num_results = $query->num_rows();
        if($num_results > 0)
        {
            return true;
        }
        return false; 
    }
    public function is_logged_in()
    {
        if($this->session->userdata('user_id') !== false)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public function get_id()
    {
        return $this->session->userdata('user_id');
    }
}